package com.gps.skyrc.tool;

import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.gps.skyrc.tool.logger.ILogger;


public class Tip {


    private static String oldMsg;
    protected static Toast toast   = null;
    private static long oneTime=0;
    private static long twoTime=0;

    public static void showToast(Context context, String s){
        if(toast==null){
            toast =Toast.makeText(context, s, Toast.LENGTH_SHORT);
            toast.show();
            oneTime=System.currentTimeMillis();
        }else{
            twoTime=System.currentTimeMillis();
            if(s.equals(oldMsg)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    toast.show();
                }
            }else{
                oldMsg = s;
                toast.setText(s);
                toast.show();
            }
        }
        oneTime=twoTime;
    }


    public static void showToast(Context context, int resId){
        showToast(context, context.getString(resId));
    }



    private final static String TAG = "Tip";
    static CustomProgressDialog mLoadDialog;

    /**
     * 显示Dialog
     *
     * @param context
     */
    public static void showLoadDialog(Context context) {
        if (context == null) {
            return;
        }
        if (null != mLoadDialog && mLoadDialog.isShowing()) {
            return;
        }
        mLoadDialog = new CustomProgressDialog(context);
        mLoadDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
               // LogUtil.d(TAG,"Dialog隐藏了");
            }
        });

        try {
            mLoadDialog.show();
        } catch (Exception e) {
            ILogger.d(e.getMessage());
            e.printStackTrace();
        }
    }

    public static void closeLoadDialog() {
        try {
            //当dialog所在的Activity已经finish，如果再dismiss就会报错
            if (null != mLoadDialog && mLoadDialog.isShowing()) {
                mLoadDialog.dismiss();
                mLoadDialog = null;
            }
        } catch (Exception e) {
            //这里有可能会报IllegalArgumentException异常
            e.printStackTrace();
        }
    }

}
